package com.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class SwtTable {
	// swt 表格的编辑的语法：

	public static void main(String[] args) {
		final Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("table放在选项框中的实例");

		// sheet1
		TabFolder sheet1 = new TabFolder(shell, SWT.NONE);
		sheet1.setBounds(5, 5, 1000, 700);
		TabItem item1 = new TabItem(sheet1, SWT.NONE);
		item1.setText("sheet1");

		Composite composite1 = new Composite(sheet1, SWT.NONE);
		composite1.setBounds(5, 5, 180, 130);
		final Table table1 = new Table(composite1, SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
		table1.setBounds(0, 0, 900, 600);
		table1.setVisible(true);
		table1.setLinesVisible(true);
		// table1.setItemCount(10);//?
		TableColumn[] columns1 = new TableColumn[10];
		for (int i = 0; i < 10; i++) {
			columns1[i] = new TableColumn(table1, SWT.CENTER);
			columns1[i].setText(i + "");
			// columns1[i].setResizable(true);//?
			columns1[i].setWidth(70);

		}
		TableItem temp = null;
		for (int i = 0; i < 10; i++) {
			temp = new TableItem(table1, SWT.NONE);
			for (int j = 0; j < 10; j++) {
				temp.setText(j, i + "" + j);
			}
		}

		item1.setControl(composite1);

		table1.addListener(SWT.MouseDoubleClick, new Listener() {
			@Override
			public void handleEvent(Event event) {
				Point point = new Point(event.x, event.y);
				TableItem itm = table1.getItem(point);
				if (itm == null)
					return;
				for (int i = 0; i < 10; i++) {
					Rectangle rect = itm.getBounds(i);
					if (rect.contains(point)) {
						System.out.println("列值" + i);
						final TableEditor editor = new TableEditor(table1);
						Control oldEditor = editor.getEditor();
						if (oldEditor != null)
							oldEditor.dispose();
						Text textEditor = new Text(table1, SWT.NONE);
						textEditor.computeSize(SWT.DEFAULT,
								table1.getItemHeight());
						editor.grabHorizontal = true;
						editor.minimumHeight = textEditor.getSize().y;
						editor.minimumWidth = textEditor.getSize().x;
						editor.setEditor(textEditor, itm, i);
						textEditor.setFont(new Font(display, "宋体", 10,
								SWT.ITALIC));
						textEditor.setText(itm.getText(i));
						textEditor.forceFocus();
						final int myi = i;
						textEditor.addModifyListener(new ModifyListener() {
							@Override
							public void modifyText(ModifyEvent e) {
								Text text = (Text) editor.getEditor();
								text.setForeground(display
										.getSystemColor(SWT.COLOR_RED));
								editor.getItem().setText(myi, text.getText());
							}
						});
						textEditor.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
							@SuppressWarnings("unused")
							@Override
							public void focusLost(FocusEvent e) {
								System.out.println("失去焦点");
								String text = ((Text) editor
										.getEditor()).getText();
								editor.getEditor().dispose();
								TableItem[] is = table1.getItems();
								for (int i = 0; i < is.length; i++) {
									for (int j = 0; j < 10; j++) {
										System.out.print(is[i]
												.getText(j) + " ");
									}
									System.out.println();
								}
							}
						});
					}
				}
			}
		});
		TabItem item2 = new TabItem(sheet1, SWT.NONE);
		item2.setText("sheet2");
	
		Label l2 = new Label(sheet1, SWT.NONE);
		l2.setBounds(5, 20, 40, 40);
		l2.setText("第二个2");
		item2.setControl(l2);
	
		// shell.pack();
		shell.open();
	
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
